<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-select v-model="optionsVal" placeholder="查询条件">
          <el-option
            v-for="item in options"
            :key="item.label"
            :label="item.value"
            :value="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="keyword" placeholder="查询值" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('manage:tcourseinformation:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >添加课程</el-button
        >
        <el-button
          v-if="isAuth('manage:tcourseinformation:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
        <el-button
          type="success"
          v-if="isAuth('manage:tcourseinformation:study')"
          @click="stady"
          >学习课程</el-button
        >
        <el-button
          v-if="isAuth('manage:tcourseinformation:import')"
          type="warning"
          @click="importHandle()"
          >导入</el-button
        >
        <el-button
          v-if="isAuth('manage:tcourseinformation:export')"
          type="info"
          @click="exportHandle()"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="courseCode"
        header-align="center"
        align="center"
        label="课程编号"
      >
      </el-table-column>
      <el-table-column
        prop="courseName"
        header-align="center"
        align="center"
        label="课程名称"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="courseDesciption"
        header-align="center"
        align="center"
        label="课程图片">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
      </el-table-column> -->
      <el-table-column prop="courseDescription" label="课程图片" align="center">
        <template slot-scope="scope">
          <img
            v-if="scope.row.courseDescription"
            :src="getImage(scope.row.courseDescription)"
            width="60"
            height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="courseAddress"
        header-align="center"
        align="center"
        label="课程地址"
      ></el-table-column>
      <el-table-column
        prop="courseContent"
        header-align="center"
        align="center"
        label="课程内容"
      >
      </el-table-column>
      <el-table-column
        prop="coursePeopleAmount"
        header-align="center"
        align="center"
        label="上课人数"
      >
      </el-table-column>
      <el-table-column
        prop="courseStartTime"
        header-align="center"
        align="center"
        label="上课时间"
      >
      </el-table-column>
      <el-table-column
        prop="courseTeacherCode"
        header-align="center"
        align="center"
        label="任课老师编号"
      >
      </el-table-column>
      <el-table-column
        prop="courseTeacherName"
        header-align="center"
        align="center"
        label="任课老师名称"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="isAuth('manage:tcourseinformation:update')"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="isAuth('manage:tcourseinformation:delete')"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <el-dialog title="导入" :visible.sync="dialogImportVisible" width="480px">
      <el-form label-position="right" label-width="170px">
        <el-form-item label="文件">
          <el-upload
            :multiple="false"
            :on-success="onUploadSuccess"
            :action="'http://localhost:88/api/course/tcourseinformation/import'"
            class="upload-demo"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传xls文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogImportVisible = false"> 取消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./tcourseinformation-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      options: [
        {
          value: "课程编号",
          label: "course_code",
        },
        {
          value: "课程名称",
          label: "course_name",
        },
        {
          value: "任课老师编号",
          label: "teaching_course_code",
        }, {
          value: "任课老师姓名",
          label: "teaching_course_name",
        }
      ],
      value: "",
      param: "",
      optionsVal: "",
      keyword: "",
      dialogImportVisible:false
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      // this.param =param;

      let custParams = this.$http.adornParams({
        page: this.pageIndex,
        limit: this.pageSize,
      });
      if (this.optionsVal != null && this.keyword != null) {
        custParams[this.optionsVal] = this.keyword;
      }
      this.$http({
        url: this.$http.adornUrl("/course/tcourseinformation/list"),
        method: "get",
        params: custParams,
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },

    exportHandle() {
      window.location.href =
        "http://localhost:88/api/course/tcourseinformation/export";
    },
    importHandle() {
      this.dialogImportVisible = true;
    },
    onUploadSuccess(response, file) {
      this.$message.info("导入成功");
      this.dialogImportVisible = false;
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },

    stady() {
      console.log(this.dataListSelections);
      this.$http({
        url: this.$http.adornUrl("/course/tcourseinformation/study"),
        method: "post",
        data: this.dataListSelections,
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
          
          this.$router.push('i-67')
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    getImage(courseDescription) {
      return `http://localhost:88/api/course/common/download?name=${courseDescription}`;
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定进行操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/course/tcourseinformation/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
